package com.taxshare.operation.service;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.ResourceEntity;
import com.taxshare.operation.mapper.ResourceMapper;
import com.taxshare.operation.view.dto.ResourceDTO;
import com.taxshare.operation.view.vo.ResourceVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @ClassName: ResourceService
 * @Description:
 * @author: luozhongyi
 * @date: 2021/1/29 14:13
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class ResourceService extends ServiceImpl<ResourceMapper, ResourceEntity> {

    /**
     * 获取资源类型列表
     */
    public Response getResourceTypeCode() {
        List<Map<String, String>> list = new ArrayList();
        list.add(buildMap("模块", "T001", "menu"));
        list.add(buildMap("菜单", "T002", "menu"));
        list.add(buildMap("链接", "T003", "menu"));
        list.add(buildMap("按钮", "T004", "button"));
        list.add(buildMap("页签", "T005", "button"));
        return Response.success(list);
    }

    /**
     * 初始化左侧资源树
     * @return
     */
    public Response initTree() {
        QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper();
        //排除按钮和页签
        queryWrapper.notIn("resource_type", Arrays.asList("T004","T005"));
        queryWrapper.orderByAsc("resource_sort");
        queryWrapper.orderByDesc("create_time");
        List<ResourceEntity> resourceList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(resourceList)) {
            return Response.success(resourceList);
        }
        List<ResourceVO> resourceVOList = JSONUtil.toList(JSONUtil.parseArray(resourceList), ResourceVO.class);
        //构造菜单树
        Map<String, ResourceVO> dataMap = new HashMap<>(resourceVOList.size() * 4 / 3 + 1);
        for (ResourceVO bean : resourceVOList) {
            dataMap.put(bean.getResourceId(), bean);
        }
        //构造Tree
        List<ResourceVO> treeList = new ArrayList<>(30);
        for (ResourceVO bean : resourceVOList) {
            //如果父节点为0,则表示其为根节点
            if ("0".equals(bean.getResourceParentId())) {
                treeList.add(bean);
            } else {
                //获取父节点所在的对象
                ResourceVO parentEntity = dataMap.get(bean.getResourceParentId());
                //如果为null则表明此节点的父节点不存在了
                if (parentEntity != null) {
                    //将其存放到父节点的children属性下
                    parentEntity.getChildren().add(bean);
                }
            }
        }
        //外层添加根节点
        List<ResourceVO> returList = new ArrayList<>(30);
        ResourceVO resourceBean = new ResourceVO();
        resourceBean.setResourceId("0");
        resourceBean.setResourceParentId("-1");
        resourceBean.setResourceName("资源菜单");
        resourceBean.setChildren(treeList);
        returList.add(resourceBean);
        return Response.success(returList);
    }

    /**
     *
     * @param resourceId
     * @return
     */
    public Response getDetail(String resourceId) {
        ResourceEntity resourceEntity = baseMapper.selectById(resourceId);
        return Response.success(resourceEntity);
    }


    /**
     *
     * @param resourceId
     * @return
     */
    public Response getButton(String resourceId) {
        QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("resource_parent_id",resourceId);
        queryWrapper.in("resource_type",Arrays.asList("T004", "T005"));
        queryWrapper.orderByAsc("resource_sort");
        queryWrapper.orderByDesc("create_time");
        List<ResourceEntity> resourceEntitys = baseMapper.selectList(queryWrapper);
        List<ResourceVO> resourceVOs = DozerBeanUtil.mapList(resourceEntitys,ResourceVO.class);
        //外层添加根节点
        List<ResourceVO> returList = new ArrayList<>(30);
        ResourceVO resourceVO = new ResourceVO();
        resourceVO.setResourceId(resourceId);
        resourceVO.setResourceParentId("-2");
        resourceVO.setResourceName("按钮资源");
        resourceVO.setChildren(resourceVOs);
        returList.add(resourceVO);
        return Response.success(returList);
    }

    /**
     *
     * @param resourceDTO
     * @return
     */
    public Response saveInfo(ResourceDTO resourceDTO) {
        ResourceEntity resourceEntity = DozerBeanUtil.map(resourceDTO,ResourceEntity.class);
        resourceEntity.setResourceId(UUIDUitl.UUID32(false));
        baseMapper.insert(resourceEntity);
        return Response.success();
    }

    /**
     *
     * @param resourceDTO
     * @return
     */
    public Response updateInfo(ResourceDTO resourceDTO) {
        resourceDTO.setCreateTime(new Date());
        resourceDTO.setUpdateTime(new Date());
        ResourceEntity resourceEntity = DozerBeanUtil.map(resourceDTO,ResourceEntity.class);
        baseMapper.updateById(resourceEntity);
        return Response.success();
    }

    /**
     *
     * @param resourceId
     * @return
     */
    public Response deleteInfo(String resourceId) {
         baseMapper.deleteById(resourceId);
        return Response.success();
    }

    /**
     * 组装对象
     */
    private Map<String, String> buildMap(String name, String value, String content) {
        Map<String, String> map = new HashMap<>(8);
        map.put("name", name);
        map.put("value", value);
        map.put("content", content);
        return map;
    }
}
